/**
 * Created by Jone on 2016/7/15.
 */

App.config(['$stateProvider', '$locationProvider', '$urlRouterProvider', 'RouteHelpersProvider',
    function ($stateProvider, $locationProvider, $urlRouterProvider, helper) {
        $stateProvider
            .state('app-api.setting_project', {
                url: '/setting/project',
                title: 'Single View',
                controller: 'SettingController',
                templateUrl: helper.basepath('setting/project.html'),
                resolve: helper.resolveFor( 'toaster')
            })
    }]);


App.controller('SettingController', function ($scope, $rootScope, $cookieStore, $http, $location, $window, $state, $timeout, toaster) {

    $scope.project = $cookieStore.get("project");
    $scope.deleteProject = function() {
        $http.delete("/projects/" + $cookieStore.get("project")._id)
            .success(function (response) {
                if (response.code == 0) {
                    toaster.pop('success', '提示', '删除成功');
                    $cookieStore.remove("project");
                    $timeout(function(){$state.go('app.project_list_all')}, 2000 );
                }
            });
    }

    $scope.updateProject = function() {
        $http.put("/projects", $scope.project)
            .success(function (response) {
                if (response.code == 0) {
                    $cookieStore.put("project", response.data);
                    toaster.pop('success', '提示', '修改成功');
                }
            });
    }

    $scope.select = function(project) {
        console.log(JSON.stringify(project));
        $cookieStore.put("project", project);
        $state.go('app-api.group_list_all');
    }

    $scope.list = function() {
        $http.get("/projects")
            .success(function (response) {
                $scope.projects = response.data;
            })
            .error(function(data) {

            });
    }

    $scope.delete = function(project) {
        $http.delete("/projects/" + project._id)
            .success(function (response) {
                if (response.code == 0) {
                    toaster.pop('success', '提示', '删除成功');
                    $scope.list();
                }
            });
    }

    //$scope.list();

});